/*
 	PureMVC UnifiedLoader Utility
 	Copyright (c) 2008 neil manuell
  	Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
 package org.puremvc.as3.multicore.utilities.unifiedloader.patterns.factories
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import org.puremvc.as3.multicore.utilities.unifiedloader.errors.FactoryReturnError;
	import org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.ICreate;
	

	
	public class DisplayObjectFactory implements ICreate
	{
		private var mAsset:DisplayObject
		
		public function DisplayObjectFactory(asset:DisplayObject)
		{
			mAsset = asset;
		}
		
		public function create(id:String = "", asClass:Boolean=false):*
		{
			var clazz:Class
			
			switch(true)
			{
				case(id == "asset"):
					return mAsset;
					
				case(mAsset is Sprite && !asClass):
					clazz = Class(mAsset.loaderInfo.applicationDomain.getDefinition(id));
					return new clazz();
					
				case(mAsset is Sprite && asClass):
					clazz = Class(mAsset.loaderInfo.applicationDomain.getDefinition(id));
					return clazz;

				default:
					throw new FactoryReturnError(FactoryReturnError.NULL_RETURN);
					
			}
			
			
		}

	}
}